package data_structure.helper;

import java.util.Random;

public class ArrayGenerator {

    private ArrayGenerator() {

    }

    /**
     * generate an array which is ordered
     * @param n the length of array
     * @return the target array
     */
    public static Integer [] generateOrderedArray(int n){

        Integer[] array = new Integer[n];

        for (int i = 0; i < n; i++) {
            array[i]=i;
        }
        return array;
    }

    /**
     * 生成字符串数组，数量为n，字符串的长度为w
     * @param n
     * @param w
     * @return
     */
    public static String[] generateSameLengthRandomString(int n,int w){

        String []arr=new String[n];
        Random random = new Random();
        for(int i=0;i<arr.length; i++){
            StringBuilder sb = new StringBuilder();
            //可打印字符：32-127
            for(int j=0;j<w;j++){
                sb.append ((char)(32+random.nextInt(96)));
            }
            arr[i]=sb.toString ();
        }
        return arr;
    }
    public static String[] generateRandomString(int n, int bound) {

        String []arr = new String[n];
        Random random = new Random();
        for(int i=0;i<arr.length;i++){

            StringBuilder sb=new StringBuilder();
            int w=random.nextInt (bound);
            for(int j=0;j<=w;j++){
                sb.append ((char)(32+random.nextInt (96)));
            }
            arr[i]=sb.toString ();
        }
        return arr;
    }

    /**
     * generate an array which is not ordered
     * @param n the length of array
     * @param bound the max value in the array
     * @return an array which is not ordered
     */
    /**
     * 生成一组特殊的数组，快速排序的特列
     *  @param n
     * @param bound
     * @return
     */
    public static Integer[] generateNormalArray(int n,int bound){

        Integer result[] = new Integer[n];
        Random random = new Random();
        for(int i = 0; i < n; i++){
            result[i]=random.nextInt(bound);
        }
        return result;
    }

    public static Integer[] generateSpecialArray(int bound){
        //0,2,3,4....n-1
        Integer[] result =ArrayGenerator.generateOrderedArray (bound);
        adjust(result,0);
        return result;
    }

    private static void adjust(Integer []arr,int left){

        if(left>=arr.length-1)return;
        int mid=(left+arr.length-1)/2;
        SwapArray.swap (arr,left,mid);
        adjust (arr,left+1);
    }
}
